import bankSystem
import random


# 创建账户
def create_account(system):
    account_number = random.randint(100000, 1000000)
    account_holder = input('请输入您的用户名：')
    initial_balance = 0
    ret = system.create_account(account_number, account_holder, initial_balance)
    if ret:
        print(f'用户:{account_holder},恭喜您开户成功!您的卡号为:{account_number}')
    else:
        print("用户名已存在，开户失败！")


# 存款
def deposit(system):
    account_number = int(input('请输入您要存款的账号：'))
    money = int(input('请输入您要存款的金额：'))
    account = system.get_account(account_number)
    account.deposit(money)
    print('存款成功！')
    account.display_info()


# 取款
def withdraw(system):
    account_number = int(input('请输入您要取款的账号：'))
    account = system.get_account(account_number)
    if account:
        money = int(input('请输入您要取款的金额：'))
        ret = account.withdraw(money)
        if ret:
            print('取款成功！')
            account.display_info()
        else:
            print('余额不足取款失败！')
    else:
        print('账户不存在取款失败！')


# 转账
def transfer(system):
    from_ID = int(input('请输入您的卡号：'))
    to_ID = int(input('请输入您要转账的卡号：'))
    money = int(input('请输入您要转账的金额：'))
    from_account = system.get_account(from_ID)
    to_account = system.get_account(to_ID)
    ret = from_account.transfer(to_account, money)
    if ret:
        print('转账成功！')
        from_account.display_info()
        to_account.display_info()
    else:
        print('账户余额不足，转账失败！')


# 查询余额
def get_balance(system):
    account_number = int(input('请输入您的卡号：'))
    account = system.get_account(account_number)
    if account:
        account.display_info()
    else:
        print('账户不存在，查询失败！')


# 打印菜单
def menu():
    print('-' * 20)
    print('欢迎来到银行管理系统！')
    print('1、创建账户')
    print('2、存款')
    print('3、取款')
    print('4、转账')
    print('5、查询余额')
    print('0、退出系统')
    print('-' * 20)


def main(choice):
    # 1、创建账户
    if choice == '1':
        create_account(system)
    # 2、存款
    elif choice == '2':
        deposit(system)
    # 3、取款
    elif choice == '3':
        withdraw(system)
    # 4、转账
    elif choice == '4':
        transfer(system)
    # 5、查询余额
    elif choice == '5':
        get_balance(system)
    # 0、退出系统
    elif choice == '0':
        print('退出银行管理系统！')
        exit()


system = bankSystem.BankSystem()
while 1:
    menu()
    choice = input('请选择您要执行的操作：')
    main(choice)
